/**
 * 家庭描述
 */
package com.findu.model;

import java.util.ArrayList;

/**
 *
 * @author 夏启根
 */
public class FUFamilyTree {

    public static String surname;       //  族谱姓氏

    public FUFamilyMember homeOwner = null;     //  家主
//    public FUMateTree nowMate = null;             //  现任配偶()
    public ArrayList<FUMateTree> mates = null;     //  配偶
    public ArrayList<FUFamilyTree> childrens = null;        //  收养子女
    public FUFamilyTree parent = null;      //  上辈
    public boolean isFemaleTie = false;       //  母系（招亲的）

    public int x;

    /**
     * 在添加兄弟关系是，要先虚拟个无家主的实例来。
     */
    public FUFamilyTree() {

    }

    public int getLevel(int cur) {
        int l = cur + 1, relt;
        if (this.homeOwner == null) {
            return 0;    //  无家主，无效家庭树。
        }
        if (this.childrens != null) {
            for (FUFamilyTree sub : this.childrens) {
                relt = sub.getLevel(cur + 1);
                if (relt > l) {
                    l = relt;
                }
            }
        }
        if (this.mates != null) {
            for (FUMateTree p : this.mates) {
                if (p.childrens != null) {
                    for (FUFamilyTree sub : p.childrens) {
                        relt = sub.getLevel(cur + 1);
                        if (relt > l) {
                            l = relt;
                        }
                    }
                }
            }
        }
        return l;
    }

    public void getChilds(int cur, int[] total) {
        if (this.homeOwner == null) {
            return;    //  无家主，无效家庭树。
        }
        //  计算本级
        total[cur]++;
        if (this.mates != null) {
            total[cur] += this.mates.size();
        }
        //计算儿女
        if (this.childrens != null) {
            for (FUFamilyTree sub : this.childrens) {
                sub.getChilds(cur + 1, total);
            }
        }
        if (this.mates != null) {
            for (FUMateTree p : this.mates) {
                if (p.childrens != null) {
                    for (FUFamilyTree sub : p.childrens) {
                        sub.getChilds(cur + 1, total);
                    }
                }
            }
        }

    }

    /**
     * 默认男性为家主
     *
     * @param m
     */
    public FUFamilyTree(FUFamilyMember m) {
        this.homeOwner = m;
        if (FUFamilyMember.SEX_Man == m.sex) {
            this.isFemaleTie = false;
        } else {
            this.isFemaleTie = true;
        }
    }

    /**
     *
     * @param m
     */
    public FUFamilyTree(FUFamilyMember m, boolean is) {
        if (is) {
            this.homeOwner = m;
        } else {
            this.mates = new ArrayList<FUMateTree>();
            this.mates.add(new FUMateTree(m));
        }
        if (FUFamilyMember.SEX_Man == m.sex) {
            if (is) {
                this.isFemaleTie = false;
            } else {
                this.isFemaleTie = true;
            }
        } else {
            if (is) {
                this.isFemaleTie = true;
            } else {
                this.isFemaleTie = false;
            }
        }
    }

    public void setFamilyOwner(FUFamilyMember m) {
        this.homeOwner = m;
        if (FUFamilyMember.SEX_Man == m.sex) {
            this.isFemaleTie = false;
        } else {
            this.isFemaleTie = true;
        }
    }

    public void setFamilyMate(FUFamilyMember m) {
        if (this.mates == null) {
            this.mates = new ArrayList<FUMateTree>();
        }
        this.mates.add(new FUMateTree(m));
    }

    public void setFamilyMember(FUFamilyMember m) {
        if (this.homeOwner == null) {
            this.setFamilyOwner(m);
        } else {
            this.setFamilyMate(m);
        }
    }

    /**
     * 查找整个树。
     *
     * @param m
     * @return
     */
    public FUFamilyTree findFUFamilyTreeByMember(FUFamilyMember m) {
        FUFamilyTree node = null;
        if (this.homeOwner == null) {
            return null;
        }
        if (this.homeOwner.id.equals(m.id)) {
            return this;
        }
        if (this.childrens != null) {
            for (FUFamilyTree sub : this.childrens) {
                FUFamilyTree relt = sub.findFUFamilyTreeByMember(m.id);
                if (relt != null) {
                    return relt;
                }
            }
        }
        // 查配偶
        if (this.mates != null) {
            for (FUMateTree mate : this.mates) {
                if (mate.id.equals(m.id)) {
                    return this;
                }
                node = mate.findFUFamilyTreeByMember(m.id);
                if (node != null) {
                    return node;
                }
            }
        }
        return node;
    }

    public FUFamilyTree findFUFamilyTreeByMember(String id) {
        FUFamilyTree node = null;
        if (this.homeOwner == null) {
            return null;
        }
        if (this.homeOwner.id.equals(id)) {
            return this;
        }
        if (this.childrens != null) {
            for (FUFamilyTree sub : this.childrens) {
                node = sub.findFUFamilyTreeByMember(id);
                if (node != null) {
                    return node;
                }
            }
        }
        // 查配偶
        if (this.mates != null) {
            for (FUMateTree mate : this.mates) {
                if (mate.id.equals(id)) {
                    return this;
                }
                node = mate.findFUFamilyTreeByMember(id);
                if (node != null) {
                    return node;
                }
            }
        }
        return node;
    }

    public boolean isHaveChild() {
        if (this.isHaveMate()) {
            for (FUMateTree mate : this.mates) {
                if (mate.childrens != null && mate.childrens.isEmpty() == false) {
                    return true;
                }
            }

        }

        if (this.childrens == null) {
            return false;
        } else if (this.childrens.isEmpty()) {
            return false;
        } else {
            return true;
        }
    }

    public boolean isHaveMate() {
        if (this.mates == null) {
            return false;
        }
        if (this.mates.isEmpty()) {
            return false;
        }
        return true;
    }

    @Override
    protected void finalize() throws Throwable {
        super.finalize();
        if (this.childrens != null) {
            this.childrens.clear();
        }
        if (mates != null) {
            for (FUMateTree mate : mates) {
                if (mate.childrens != null) {
                    mate.childrens.clear();
                }
            }
        }
    }

}
